{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import subprocess"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "! apt-get install ffmpeg"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "! wget https://gif.ski/gifski-0.10.1.zip\n",
    "! unzip gifski-0.10.1.zip"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "os.chdir('linux')\n",
    "! dpkg -i gifski_0.10.1_amd64.deb"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "os.chdir('/content')\n",
    "! git init\n",
    "! git remote add -f origin https://github.com/fengredrum/tiny-gif-converter.git\n",
    "! git config core.sparsecheckout true\n",
    "! echo sample >> .git/info/sparse-checkout\n",
    "! git pull origin master"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def gif_converter(\n",
    "    load_path=None,\n",
    "    save_path=None,\n",
    "    start_time='00:00:00',\n",
    "    duration='5.',\n",
    "    fps='10',\n",
    "    frame_width='320',\n",
    "):\n",
    "\n",
    "    # Setup path\n",
    "    if load_path is None:\n",
    "        load_path = 'sample.mp4'\n",
    "    if save_path is None:\n",
    "        save_path = 'output/'\n",
    "        os.makedirs(save_path, exist_ok=True)\n",
    "\n",
    "    subprocess.call(['mkdir', 'tmp/'])\n",
    "\n",
    "    # Extract frames\n",
    "    subprocess.call([\n",
    "                    'ffmpeg',\n",
    "                    '-ss', start_time,  # start time\n",
    "                    '-t', duration,  # duration\n",
    "                    '-i', load_path,  # original file dir\n",
    "                    '-vf', \"fps=\" + fps + \", scale=\" + frame_width + \":-1\",\n",
    "                    '-pix_fmt', 'rgb24',  # pixel formats\n",
    "                    # output file dir\n",
    "                    '-y', 'tmp/frame%04d.png',\n",
    "                    ])\n",
    "\n",
    "    # Create GIF\n",
    "    out_file_name = os.path.basename(load_path) + '_converted.gif'\n",
    "    ex = subprocess.Popen(\"gifski --fps \" + fps + \" -o \" +\n",
    "                          os.path.join(save_path, out_file_name) +\n",
    "                          \" tmp/frame*.png\", \n",
    "                          stdout=subprocess.PIPE,\n",
    "                          universal_newlines=True,\n",
    "                          shell=True)\n",
    "\n",
    "    while ex.poll() == None:\n",
    "        # yield ex.stdout.readline()\n",
    "        print(ex.stdout.readline())\n",
    "    status = ex.wait()\n",
    "    subprocess.call(['rm', '-rf', 'tmp/'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "load_path = 'sample/sample_2.mp4'\n",
    "gif_converter(load_path=load_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from IPython.display import Image\n",
    "out_path = os.path.join('output', os.path.basename(load_path) + '_converted.gif')\n",
    "Image(open(out_path,'rb').read())"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": 3
  },
  "orig_nbformat": 2
 },
 "nbformat": 4,
 "nbformat_minor": 2
}